package com.studychen.appfxcp.commom.view;

import com.studychen.appfxcp.commom.components.CpMenuItem;
import com.studychen.appfxcp.commom.components.CustomIconEnum;
import com.studychen.appfxcp.commom.model.BookkeepingListModel;
import com.studychen.appfxcp.commom.model.BookkeepingViewModel;
import com.studychen.appfxcp.commom.model.MainViewModel;
import de.saxsys.mvvmfx.*;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.layout.*;

import javafx.fxml.Initializable;
import lombok.Data;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author :chenping
 * @date :Created in 2023/6/27 14:33
 */
@Data
public class MainView implements FxmlView<MainViewModel>, Initializable {

    @InjectViewModel
    private MainViewModel mainViewModel;

    @FXML
    private StackPane centerContent;

    @FXML
    private VBox leftContent;

    @FXML
    private BorderPane mainContent;

    @FXML
    private StackPane rootContent;

    @FXML
    private AnchorPane titleContent;

    private double xoffset, yoffset;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        titleContent.setStyle("-fx-background-color: #ffffff;");
        Button close = new Button("X");
        close.setPrefWidth(20.0);
        close.setPrefHeight(20.0);
        close.setStyle("-fx-background-color: #409EFF; -fx-text-fill: #ffffff; -fx-background-radius: 5");
        // 关闭
        close.setOnAction(event -> MvvmFX.getNotificationCenter().publish("exit"));
        close.setOnMouseEntered(e -> close.setOpacity(0.5));
        close.setOnMouseExited(e -> close.setOpacity(1.0));
        close.setCursor(Cursor.DEFAULT);

        AnchorPane.setRightAnchor(close, 20.0);
        AnchorPane.setTopAnchor(close, 5.0);
        titleContent.getChildren().add(close);

        CpMenuItem bookkeeping = new CpMenuItem("记账簿", CustomIconEnum.edit);
        bookkeeping.setOnClick(e -> {
            MvvmFX.getNotificationCenter().publish("bookkeeping");
        });


        CpMenuItem bookkeepingList = new CpMenuItem("记账表", CustomIconEnum.exception);
        bookkeepingList.setOnClick(e -> {
            MvvmFX.getNotificationCenter().publish("bookkeepingList");
        });

        CpMenuItem setting = new CpMenuItem("设置", CustomIconEnum.setting);
        // 统计
        CpMenuItem statistics = new CpMenuItem("统计", CustomIconEnum.lineChart);

        leftContent.setPadding(new Insets(10.0, 0.0, 10.0, 0.0));
        leftContent.setSpacing(10.0);
        leftContent.setAlignment(Pos.TOP_CENTER);
        leftContent.setStyle("-fx-background-color: rgba(0.5,0.5,0.5,0.5);");
        leftContent.getChildren().addAll(bookkeeping, bookkeepingList, statistics, setting);

        // 注册监听事件
        messageDeal();

        bookkeeping.click();
    }

    public void messageDeal(){

        // 打开列表页
        MvvmFX.getNotificationCenter().subscribe("bookkeepingList", (key, payload) -> {
            // trigger some actions
            Platform.runLater(() -> {
                ViewTuple<BookkeepingListView, BookkeepingListModel> viewTuple = FluentViewLoader.fxmlView(BookkeepingListView.class).load();
                Parent bookkeepingView = viewTuple.getView();
                centerContent.getChildren().clear();
                centerContent.getChildren().add(bookkeepingView);
            });
        });

        // 打开记账页
        MvvmFX.getNotificationCenter().subscribe("bookkeeping", (key, payload) -> {
            // trigger some actions
            Platform.runLater(() -> {
                ViewTuple<BookkeepingView, BookkeepingViewModel> viewTuple = FluentViewLoader.fxmlView(BookkeepingView.class).load();
                Parent bookkeepingView = viewTuple.getView();
                centerContent.getChildren().clear();
                centerContent.getChildren().add(bookkeepingView);
            });
        });


    }
}
